import { ExportToExcel, ImportExcel } from "../ExcelGrid/classes/ExcelService";
import { arrayDeepLevel } from "../../tools/Tools";
/**计算签字行内容
 * @param signs
 * @returns text
 */
function calculateSign(tableWidth, signs, signDirection) {
    // const tableWidth: any =
    //   document.querySelector(".handsontable .wtHider")["offsetWidth"] - 50; //表格总宽度，-50，需要减去序号的宽度
    let total = signDirection === "horizontal"
        ? 99 * (tableWidth / 1383)
        : 62 * (tableWidth / 850); //签字行带空格横向99个字，纵向62个字,再乘打印时实际放大或缩小的系数
    let signTexts = "";
    let signTextTotal = 0;
    for (let i = 0; i < signs.length; i++) {
        signTextTotal += signs[i].length;
    }
    //纵向宽度的需要单独处理
    if (tableWidth < 690) {
        total = 62 * (tableWidth / 785);
    }
    console.log(tableWidth, "tableWidth-----");
    console.log(total, "total---signTextTotal", signTextTotal);
    let spaceCount = (total - signTextTotal - 6) / 2;
    console.log(spaceCount, "spaceCount---", signDirection);
    if (spaceCount < 1) {
        spaceCount = 1;
    }
    for (let i = 0; i < signs.length; i++) {
        if (i < 2) {
            const space = "　".repeat(spaceCount);
            signTexts += `${signs[i]}${space}`;
        }
        else {
            signTexts += signs[i];
        }
    }
    return signTexts;
}
/**
 * 获取cpastable表格总宽度
 * @param data header数据
 * @returns tableWidth 实际宽度
 */
function getCpasTableWidth(data) {
    if (!data || !data.length) {
        return 0;
    }
    const res = data.slice(1, data.length - 1);
    let tableWidth = 0;
    for (let i = 0; i < res.length; i++) {
        const { width } = res[i].config;
        tableWidth += width;
    }
    return tableWidth;
}
/**
 *导出带标题/副标题/日期方法
 * @param self 当前refs
 * @param type 导出类型: default 带边框 ， primary不带边框
 * @param signs 签名表数据, 有数据表明带签名表
 * @param signDirection 签名表数据横向/纵向,  horizontal/vertical
 * @returns
 */
export const ExportToTitle = function (self, type, signs, signDirection) {
    type = type || "default";
    const { tableTitle, tableLfDesc, tableRiDesc, tableDate } = self.state;
    const headerService = self.handsonTableFs.current.headerService;
    const { headerColumn } = headerService.headerLevelData;
    const levelNum = arrayDeepLevel(headerService.headJson); //计算header深度
    const cpasTableWidth = getCpasTableWidth(headerColumn); //获取cpastable总宽度
    let dataLen = self?.handsonTableFs?.current?.data
        ? self?.handsonTableFs?.current?.data.length + 5
        : 0;
    console.log(levelNum, "levelNum---- header深度");
    if (levelNum >= 0) {
        dataLen = dataLen + levelNum + 1;
    }
    console.log(dataLen, "dataLen---");
    // 获取表格容器元素
    const spaceRows = headerColumn.length && headerColumn.length > 3
        ? headerColumn.length - 3
        : 0; //根据总列计算要添加的空白列，-3是因为有其他文字占位
    const isSign = signs && signs.length ? true : false;
    const borderStyleWhite = {
        style: "thin",
        color: { rgb: "ffffff" },
    };
    let borderStyle = {
        top: { ...borderStyleWhite },
        left: { ...borderStyleWhite },
        right: { ...borderStyleWhite },
        bottom: { ...borderStyleWhite },
    };
    const fontStyle = {
        sz: "12",
        name: "仿宋_GB2312",
    };
    const alignment = {
        vertical: "center",
        horizontal: "center",
    };
    let headerTitle = []; // 表格标题
    let headerDesc = []; // 表格备注展示
    let headerDate = [];
    let signData = []; //签字行数据
    let descMergeArray = [];
    let signMergeArray = []; //签字行合并单元格数据
    tableTitle &&
        headerTitle.push({
            t: "s",
            v: tableTitle,
            s: {
                border: {
                    ...borderStyle,
                },
                alignment: { ...alignment },
                font: {
                    color: "black",
                    bold: true,
                    sz: "16",
                    name: "仿宋_GB2312",
                },
                //fill: { fgColor: { rgb: "eeeeee" }, patternType: "solid" },
            },
        });
    tableDate &&
        headerDate.push({
            t: "s",
            v: tableDate,
            s: {
                alignment: { ...alignment },
                font: {
                    ...fontStyle,
                },
                border: {
                    ...borderStyle,
                },
            },
        });
    tableLfDesc &&
        headerDesc.push({
            t: "s",
            v: tableLfDesc,
            s: {
                alignment: { vertical: "center", horizontal: "left" },
                //fill: { fgColor: { rgb: "eeeeee" }, patternType: "solid" },
                font: {
                    ...fontStyle,
                },
                border: {
                    ...borderStyle,
                },
            },
        });
    let signTexts = "";
    if (isSign && signs) {
        signTexts = calculateSign(cpasTableWidth, signs, signDirection);
    }
    isSign &&
        signData.push({
            t: "s",
            v: signTexts,
            s: {
                border: {
                    ...borderStyle,
                },
                alignment: { vertical: "center", horizontal: "left" },
                font: {
                    color: "black",
                    bold: true,
                    sz: "12",
                    name: "仿宋_GB2312",
                },
            },
        });
    //合并单元格需要
    if ((tableTitle || tableDate) && spaceRows > 0) {
        const defaultInfo = {
            t: "s",
            v: " ",
            s: {
                border: {
                    ...borderStyle,
                },
            },
        };
        for (let r = 0; r < spaceRows; r++) {
            tableTitle &&
                headerTitle.push({
                    ...defaultInfo,
                });
            tableDate &&
                headerDate.push({
                    ...defaultInfo,
                });
            isSign &&
                signData.push({
                    ...defaultInfo,
                });
        }
    }
    if (tableLfDesc && tableRiDesc && spaceRows > 1) {
        for (let r = 0; r < spaceRows - 1; r++) {
            headerDesc.push({
                t: "s",
                v: " ",
                s: {
                    border: {
                        ...borderStyle,
                    },
                },
            });
        }
    }
    tableRiDesc &&
        headerDesc.push({
            t: "s",
            v: tableRiDesc,
            s: {
                alignment: { vertical: "center", horizontal: "right" },
                //fill: { fgColor: { rgb: "eeeeee" }, patternType: "solid" },
                font: {
                    ...fontStyle,
                },
                border: {
                    ...borderStyle,
                },
            },
        });
    tableTitle &&
        descMergeArray.push({
            s: {
                c: 0,
                r: 0, //开始行
            },
            e: {
                c: spaceRows,
                r: 0,
            },
        });
    if (tableDate) {
        descMergeArray.push({
            s: {
                c: 0,
                r: 1, //开始行
            },
            e: {
                c: spaceRows,
                r: 1,
            },
        });
    }
    if (tableLfDesc || tableRiDesc) {
        descMergeArray.push({
            s: {
                c: 0,
                r: 2, //开始行
            },
            e: {
                c: spaceRows - 1,
                r: 2,
            },
        });
    }
    if (isSign) {
        signMergeArray.push({
            s: {
                c: 0,
                r: dataLen, //开始行
            },
            e: {
                c: spaceRows,
                r: dataLen,
            },
        });
    }
    let descDatas = [];
    tableTitle && descDatas.push([...headerTitle]);
    tableDate && descDatas.push([...headerDate]);
    tableLfDesc && tableRiDesc && descDatas.push([...headerDesc]);
    const HandSonGrid = self.handsonTableFs.current;
    const imoportTableTitle = {
        tableTitle,
        tableLfDesc,
        tableRiDesc,
        tableDate,
    };
    return ExportToExcel(tableTitle, {
        descDatas,
        descMergeArray,
        imoportTableTitle,
        signData,
        signMergeArray,
    }, HandSonGrid, type);
};
/**
 * 导入带标题/副标题/日期方法
 */
export const ImportToTitle = function (file, self) {
    return ImportExcel(file, self);
};
